<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Route 
 * +------------------------------------------------------------+
 * 框架内置Route类，完成对url的解析、路由和调度功能
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-17 09:21:28
 *
 */
class Route extends Base {
	static function dispath(){
		if (true === C('URL_ROUTER_ON', false)){
			$ruleFile = INCLUDES_PATH . 'route.config.php';
			//载入路由自定义规则配置文件
			if (is_file($ruleFile)){
				C('_ROUTE_RULE_', include $ruleFile);
			}
		}
		switch (C('URL_MODE')){
			case 2:      //PATH_INFO模式
				return self::pathinfo_url();
				break;
			default:     //默认路由模式
				self::_route_decode(HTTP_URL);
				return self::default_url();
		}
	}
	
	//默认url模式
	private static function default_url(){
		$mod = isset($_GET[VAR_MODULE]) ? trim($_GET[VAR_MODULE]) : '';
		$act = isset($_GET[VAR_ACTION]) ? trim($_GET[VAR_ACTION]) : '';
		unset($_GET[VAR_MODULE],$_GET[VAR_ACTION]);
		return array(
			'mod' => $mod ? $mod : DF_MODULE,
			'act' => $act ? $act : DF_ACTION
		);
	}
	
	//PATH_INFO模式
	private static function pathinfo_url(){
		$tmpUrl = self::geturl();
		if(!empty($tmpUrl) && strpos($tmpUrl, '/') === 0) $tmpUrl = substr($tmpUrl, 1);
		$request = explode(C('URL_SEPARATOR', '/'), $tmpUrl);
		$count = count($request);
		if ($count>=2){
			$mod = trim($request[0]);
			$act = trim($request[1]);
			if($count > 2){
				for($i = 2; $i < $count; $i += 2){
					if(trim($request[$i]))
						$_GET[trim($request[$i])] =  trim($request[$i+1])!='' ? trim($request[$i+1]) : '';
				}
			}
		}else{
			$mod = trim($request[0]) ? trim($request[0]) : DF_MODULE;
			$act = DF_ACTION;
		}
		return array(
			'mod' => $mod ? $mod : DF_MODULE,
			'act' => $act ? $act : DF_ACTION
		);
	}
	
	//获取当前URL
	private static function geturl(){
		$request = $_SERVER['PATH_INFO'];
		if($request){
			//$request = $_SERVER['REQUEST_URI'];
			$script = $_SERVER['SCRIPT_NAME'];
			if(substr($request, 0, strlen($script)) == $script)
				$request = str_replace($script, '', $request);
			else{
				$request = substr($request, strlen(dirname($script)));
			}
		}
		
		if($request){
			strpos($request, '?')!==false && $request = substr($request, 0, strpos($request, '?'));
			//去除伪静态后缀
			if (1 == C('PSEUDO_STATIC', 1)){
				$suffix = C('STATIC_SUFFIX', '.html');
				$start = stripos($request, $suffix);
				if (strtolower(substr($request, $start)) == strtolower($suffix))
					$request = substr($request, 0, $start);
			}
		}
		if (true === C('URL_ROUTER_ON', false)){
			//路由反解析
			$rules = C('_ROUTE_RULE_.DECODE');
			if (!empty($rules) && is_array($rules)){
				foreach ($rules as $regex => $replace){
					$request = preg_replace($regex, $replace, $request);
				}
			}
			unset($rules);
		}
		
		return $request;
	}
	
	private static function _route_decode($request){
		if (true === C('URL_ROUTER_ON', false)){
			//路由反解析
			$rules = C('_ROUTE_RULE_.DECODE');
			if (!empty($rules) && is_array($rules)){
				foreach ($rules as $regex => $replace){
					$request = preg_replace($regex, $replace, $request);
				}
			}
			unset($rules);
			$params = parse_url($request);
			if(isset($params['query'])){
				parse_str($params['query'] , $output);
				if(is_array($output)){
					foreach ($output as $key => $val){
						$_GET[$key] = $val;
					}
				}
				unset($output);
			}
			unset($params);
		}
	}
}

?>